package org.shj.springbootdemo.conf;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by Shen Huang Jian on 2017/9/9.
 */

@Configuration //在启动类中不设置@EnableWebMvc时，不要添加此注解。 
public class WebMvcConfig extends WebMvcConfigurerAdapter {

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // 注册拦截器
//        InterceptorRegistration ir = registry.addInterceptor(new LoginInterceptor());
//
//        // 配置拦截的路径
//        ir.addPathPatterns("*//**");
//
//        // 配置不拦截的路径
//        ir.excludePathPatterns("/login.html", "*.css", "*.js");
//
//        //super.addInterceptors(registry);
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
    
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer(){
    	return new EmbeddedServletContainerCustomizer(){
    		public void customize(ConfigurableEmbeddedServletContainer container){
    			ErrorPage error404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error404");
    			ErrorPage error500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error500");
    			container.addErrorPages(error404, error500);
    		}
    	};
    }
    
}
